package com.iplant.ams.controller.dms.scada;

import com.iplant.ams.service.DMSService;
import com.iplant.ams.service.po.dms.scada.record.DMSProcessRecord;
import com.iplant.ams.service.po.dms.scada.record.DMSProcessRecordItem;
import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.basic.service.po.bms.BMSEmployee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/DMSProcessRecord")
public class DMSProcessRecordController extends BaseController {

    public DMSProcessRecordController() {
        super();
    }


    @Autowired
    DMSService mDMSService;

    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            String wOrderNo = StringUtils.parseString(request.getParameter("OrderNo"));
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"), -1);
            int wTechnologyID = StringUtils.parseInt(request.getParameter("TechnologyID"), -1);
            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"), -1);
            String wDeviceNo = StringUtils.parseString(request.getParameter("DeviceNo"));


            String wAssetNo = StringUtils.parseString(request.getParameter("AssetNo"));
            String wWorkpieceNo = StringUtils.parseString(request.getParameter("WorkpieceNo"));
            int wDeviceID = StringUtils.parseInt(request.getParameter("DeviceID"), -1);

            int wActive = StringUtils.parseInt(request.getParameter("Active"), -1);
            int wStatus = StringUtils.parseInt(request.getParameter("Status"), -1);
            int wRecordType = StringUtils.parseInt(request.getParameter("RecordType"), -1);
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
            wEndTime.add(Calendar.DAY_OF_MONTH, 1);
            List<Integer> wDataClassList = StringUtils.parseIntList(request.getParameter("DataClass"), ",");
            Pagination wPagination = GetPagination(request);

            ServiceResult<List<DMSProcessRecord>> wServiceResult = mDMSService.DMS_SelectProcessRecordList(wBMSEmployee, wProductID, wOrderID, wOrderNo,
                    wAssetNo, wDeviceID, wDeviceNo, wWorkpieceNo, wRecordType, wDataClassList,
                    wActive, wStatus, wStartTime, wEndTime, wTechnologyID, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(), wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), wServiceResult.getResult(), null);
            }
        } catch (Exception ex) {
            log.error("All", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }


    @GetMapping("/Info")
    public Object Info(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            int wRecordID = StringUtils.parseInt(request.getParameter("RecordID"));

            ServiceResult<DMSProcessRecord> wServiceResult = mDMSService.DMS_SelectProcessRecord(wBMSEmployee, wRecordID);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.getResult());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServiceResult.Result);
            }
        } catch (Exception ex) {
            log.error("Info", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }

    @GetMapping("/ItemAll")
    public Object ItemAll(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {


            BMSEmployee wBMSEmployee = GetSession(request);

            String wDeviceNo = StringUtils.parseString(request.getParameter("DeviceNo"));
            int wDeviceID = StringUtils.parseInt(request.getParameter("DeviceID"), -1);
            int wRecordID = StringUtils.parseInt(request.getParameter("RecordID"), -1);
            String wParameterNo = StringUtils.parseString(request.getParameter("ParameterNo"));
            int wParameterID = StringUtils.parseInt(request.getParameter("ParameterID"), -1);
            int wActive = StringUtils.parseInt(request.getParameter("Active"), -1);
            int wStatus = StringUtils.parseInt(request.getParameter("Status"), -1);
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            Pagination wPagination = GetPagination(request);
            ServiceResult<List<DMSProcessRecordItem>> wServiceResult = mDMSService.DMS_SelectProcessRecordItemList(wBMSEmployee, wDeviceID, wDeviceNo, wRecordID,

                    wParameterID, wParameterNo, wActive, wStatus, wStartTime, wEndTime, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(), wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), wServiceResult.getResult(), null);
            }
        } catch (Exception ex) {
            log.error("ItemAll", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }

    @GetMapping("/LastInfo")
    public Object LastInfo(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            String wWorkpieceNo = StringUtils.parseString(request.getParameter("WorkpieceNo"));
            int wRecordType = StringUtils.parseInt(request.getParameter("RecordType"), -1);

            if (StringUtils.isEmpty(wWorkpieceNo)) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            ServiceResult<DMSProcessRecord> wServiceResult = mDMSService.DMS_SelectLastProcessRecord(wBMSEmployee, wWorkpieceNo, wRecordType);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.getResult());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServiceResult.Result);
            }
        } catch (Exception ex) {
            log.error("LastInfo", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }


    @GetMapping("/UploadAll")
    public Object UploadAll(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            ServiceResult<List<DMSProcessRecord>> wServiceResult = mDMSService.DMS_SelectProcessRecordUploadList(wBMSEmployee);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), wServiceResult.getResult(), null);
            }
        } catch (Exception ex) {
            log.error("UploadAll", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }

    @PostMapping("/UploadStatus")
    public Object UploadStatus(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {


            BMSEmployee wBMSEmployee = GetSession(request);
            if (!wParam.containsKey("data") || !wParam.containsKey("UploadStatus")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            int wUploadStatus = StringUtils.parseInt(wParam.get("UploadStatus"));
            List<Integer> wProcessRecordIDList = CloneTool.CloneArray(wParam.get("data"), Integer.class);
            ServiceResult<Integer> wServerRst = mDMSService.DMS_UpdateProcessRecordUploadStatus(wBMSEmployee, wProcessRecordIDList, wUploadStatus);
            // 直接插入
            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
            }
        } catch (Exception ex) {
            log.error("UploadStatus", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;

    }


    @PostMapping("/SyncProcessRecord")
    public Object SyncProcessRecord(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {


            BMSEmployee wBMSEmployee = GetSession(request);
            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            List<DMSProcessRecord> wDMSProcessRecordList = CloneTool.CloneArray(wParam.get("data"), DMSProcessRecord.class);
            ServiceResult<List<String>> wServerRst = mDMSService.DMS_SyncProcessRecordList(wBMSEmployee, wDMSProcessRecordList);
            // 直接插入
            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
            }
        } catch (Exception ex) {
            log.error("SyncProcessRecord", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;

    }


}
